package csu.web.jpetstore.web.servlet;

import csu.web.jpetstore.domain.Order;
import csu.web.jpetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ViewOrderServlet extends HttpServlet {

    private static final String VIEW_ORDER_FORM = "/WEB-INF/jsp/order/viewOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        String id = req.getParameter("orderId");
        if(id!=null){
            int orderId = Integer.parseInt(id);
            OrderService orderService = new OrderService();
            Order order = orderService.getOrder(orderId);
            session.setAttribute("orderId", id);
            session.setAttribute("order",order);
        }

        req.getRequestDispatcher(VIEW_ORDER_FORM).forward(req,resp);
    }
}
